package com.blb.controller.front;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.service.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/front/classify")
public class ClassifyController {

    @Autowired
    private IKeywordService keywordService;

    @Autowired
    private IProductTypeService productTypeService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private ICommodityService commodityService;

    @Autowired
    private IGoodstypeService goodstypeService;


    /**
     * 根据品牌获取商品信息
     * @param
     * @return
     */
    @RequestMapping("getCommodity")
    public String dasdas(@Param("brandsArr") String brandsArr, @Param("comtypeName") String comtypeName, Model model){

        //关键词根据次数排序前十条数据
        List<Keyword> keywordTop = keywordService.getKeywordTop();
        model.addAttribute("keywordTop",keywordTop);

        //获取分类的前九条数据
        List<Comtype> comtype = productTypeService.getComtype();
        model.addAttribute("comtypes",comtype);

        //根据品牌查询商品信息
        String[] brandids = brandsArr.split(",");

        List<Commodity> commodityLists = null;
        List<Brand> brandLists  = null;
        if(comtypeName!=null){
            //先将分类名称返回
            model.addAttribute("comtypeName",comtypeName);

            //根据分类查询所有商品,在查询品牌匹配的商品
            List<Comtype> comtypeByName = productTypeService.getComtypeByName(comtypeName);
            List<Goodstype> goodstypes = goodstypeService.list(new QueryWrapper<Goodstype>().eq("type_id", comtypeByName.get(0).getId()));
            List<String> comids = new ArrayList<>();
            for (Goodstype goodstype : goodstypes){
                comids.add(goodstype.getComId());
            }
            commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).in("brand_id",brandids));

            brandLists = brandService.list(new QueryWrapper<Brand>().in("id", brandids));

        }else{
            commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("brand_id", brandids));

            //显示所有的品牌
            brandLists = brandService.list(null);

        }

        model.addAttribute("commodityLists",commodityLists);
        model.addAttribute("brandLists",brandLists);


        return "front/classify";
    }

    /**
     *  综合 销量  新品
     * @return
     */
    @RequestMapping("condition")
    public String condition(String comtypeName,String brandsName,String seeekBox,Model model,String sign,String price0,String price1){


        //关键词根据次数排序前十条数据
        List<Keyword> keywordTop = keywordService.getKeywordTop();
        model.addAttribute("keywordTop",keywordTop);

        //获取分类的前九条数据
        List<Comtype> comtype = productTypeService.getComtype();
        model.addAttribute("comtypes",comtype);

        //先将分类名称返回
        model.addAttribute("comtypeName",comtypeName);

        //通过品牌名称获取品牌id
        String[] brandNames = brandsName.split(" ");
        List<Brand> brands = brandService.list(new QueryWrapper<Brand>().in("markname", brandNames));
        ArrayList<String> brandids = new ArrayList<>();
        for(Brand brand : brands){
            brandids.add(brand.getId());
        }

        //判断搜索栏是否为空,根据搜索栏中的关键字查商品，否则根据类型品牌查商品
        List<String> comids = new ArrayList<String>();
        if (seeekBox!=null && !seeekBox.equals("")){
            List<Commodity> commodities = commodityService.list(new QueryWrapper<Commodity>().like("name", seeekBox));
            for (Commodity commodity : commodities){
                comids.add(commodity.getId());
            }
            model.addAttribute("seek",seeekBox);
        }else{
            //根据分类查询所有商品,在查询品牌匹配的商品
            List<Comtype> comtypeByName = productTypeService.getComtypeByName(comtypeName);
            List<Goodstype> goodstypes = goodstypeService.list(new QueryWrapper<Goodstype>().eq("type_id", comtypeByName.get(0).getId()));
            for (Goodstype goodstype : goodstypes){
                comids.add(goodstype.getComId());
            }
        }


        List<Commodity> commodityLists = null;

        if (price0!=null && !price0.equals("") && price1!=null && !price1.equals("")){
            if(sign!=null && sign.equals("synthesize")){
                //综合
                commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).between("price",price0,price1).in("brand_id",brandids));

            }else if (sign!=null && sign.equals("sales")){
                //销量
                commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).in("brand_id",brandids).between("price",price0,price1).orderByDesc("sales"));
            }else if (sign!=null && sign.equals("newProduct")){
                //新品
                commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).in("brand_id",brandids).between("price",price0,price1).orderByDesc("putaway_time"));
            }
            model.addAttribute("price0",price0);
            model.addAttribute("price1",price1);

        }else{
            if(sign!=null && sign.equals("synthesize")){
                //综合
                commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).in("brand_id",brandids));

            }else if (sign!=null && sign.equals("sales")){
                //销量
                commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).in("brand_id",brandids).orderByDesc("sales"));
            }else if (sign!=null && sign.equals("newProduct")){
                //新品
                commodityLists = commodityService.list(new QueryWrapper<Commodity>().in("id", comids).in("brand_id",brandids).orderByDesc("putaway_time"));
            }
        }

        List<Brand> brandLists = brandService.list(new QueryWrapper<Brand>().in("id", brandids));

        model.addAttribute("sign",sign);
        model.addAttribute("commodityLists",commodityLists);
        model.addAttribute("brandLists",brandLists);

        return  "front/classify";
    }


}
